package antidestiny.java2dex.utils;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;

import antidestiny.utilib.io.FileUtils;

/**
 * Created by Hero on 2017/1/3.
 */

public class ProjectUtils {
    public static FileFilter ACCEPT_PRO_FILES =new FileFilter() {
        @Override
        public boolean accept(File pathname) {
            return pathname.isFile()&&FileUtils.suffixCompare(pathname,"pro");
        }
    };

    /**
     * 获得工程文件
     * @param code 代码文件
     */
    public static File getProject(final File code){

        //向上寻找有pro文件的文件夹
        File parent=code.getParentFile();
        File[] pro;
        while ((pro=parent.listFiles(ACCEPT_PRO_FILES))==null||pro.length==0){
            parent=parent.getParentFile();
            if (parent==null)return null;

        }
        return pro[0];

    }
    /**
     * 获取工程目录的名
     * @param dir 工程目录
     * @return 目录.异常情况下返回空
     */
    public static String projectName(File dir){
        if (dir==null||!dir.isDirectory())
            return null;
        File[] pros= dir.listFiles(ACCEPT_PRO_FILES);
        if (pros.length==0)return null;
        String proFileName=pros[0].getName();
        return proFileName.substring(0,proFileName.lastIndexOf('.'));
    }
    public static boolean isAProjectDir(File dir){
        return projectName(dir)!=null;
    }
    public static int countProjects(File workspace){
        ArrayList<File> pros=FileUtils.getAll(workspace, ACCEPT_PRO_FILES,null);
        if (pros==null)return 0;
        return pros.size();
    }
}
